package com.maven.front.controller.eduArticle;

import com.maven.front.bean.EduArticle;
import com.maven.front.bean.EduArticleContent;
import com.maven.front.bean.EduComment;
import com.maven.front.bean.Result;
import com.maven.front.dto.EduArticleDto;
import com.maven.front.dto.EduCommentDto;
import com.maven.front.service.eduArticle.EduArticleContentService;
import com.maven.front.service.eduArticle.EduArticleService;
import com.maven.front.service.eduArticle.EduCommentService1;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
@CrossOrigin //解决跨域问题
@RestController
@RequestMapping("user/e")
public class EduArticleController {
    @Autowired
    private EduArticleService eduArticleService;
    @Autowired
    private EduArticleContentService eduArticleContentService;
    @Autowired
    private EduCommentService1 eduCommentService1;



    //查询所有或者条件查询
    @GetMapping("listAll")
    public Result listAll(EduArticle eduArticle){
        List<EduArticleDto> list = eduArticleService.listAll(eduArticle);
        if (CollectionUtils.isEmpty(list)) {
            return new Result(1005, "查询失败", null);
        } else {

            return new Result(200,"查询成功",list);
        }
    }

    @GetMapping("get")
    public Result getId(Integer id){
        EduArticle eduArticle=eduArticleService.selectByPrimaryKey(id);
        //获取副表id
        int articleid=eduArticle.getArticleId();
        //查询副表
        EduArticleContent eduArticleContent=eduArticleContentService.selectid(articleid);
        //获取需要字段
        String content=eduArticleContent.getContent();
        //继承表
        EduArticleDto eduArticleDto=new EduArticleDto();
        eduArticleDto.setContent(content);
        //  将父类数据复制给子类数据
        BeanUtils.copyProperties(eduArticle,eduArticleDto);
        if (eduArticleDto==null) {
            return new Result(1007, "查询失败", null);
        } else {
            return new Result(200,"查询成功",eduArticleDto);
        }
    }


    @GetMapping("selectAll")
    public Result selectAll(Integer id){
        List<EduCommentDto> list=eduArticleService.selectAll(id);
        if (CollectionUtils.isEmpty(list)) {
            return new Result(1006, "查询失败", null);
        } else {
            return new Result(200,"查询成功",list);
        }

    }

    @PostMapping("insert1")
    public Result insert1(EduComment eduComment){
        eduComment.setAddtime(new Date());
        eduComment.setUserId(7);
        eduComment.setpCommentId(0);
        eduComment.setPraiseCount(0);
        eduComment.setReplyCount(0);
        eduComment.setType(2);
     int a= eduCommentService1.insert1(eduComment);

        if (a==1) {
            return new Result(1007, "成功" );
        } else {
            return new Result(200,"失败",null);
        }

    }


}
